<?php

namespace App\Console\Commands;

use App\Article;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Symfony\Component\DomCrawler\Crawler;

class SpiderCnbetaNews extends Command
{
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'spider:cnbetaNews';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '爬去cnbeta美剧的内容';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        //http://www.cnbeta.com/topics/545.htm
        set_time_limit(1000);
        //获取登陆cookie
        $header_params = [
            'host' => 'http://www.cnbeta.com',
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Cache-control:' => 'max-age=0',
            'Dnt' => '1',
            'Referer' => 'http://www.cnbeta.com',
            'Upgrade-Insecure-Requests' => '1',
            'User-Agent' => 'Mozilla/5.0 (compatible;Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)',
        ];
        //使用共享cookie和伪造headers
        $this->client = new Client([
            'cookies' => true,
            'headers' => $header_params,
            'http_errors' => false
        ]);
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //http://www.cnbeta.com/topics/545.htm
        $url = 'http://www.cnbeta.com/topics/545.htm';
        $res = $this->client->request('GET', $url);

        if ($res->getStatusCode() != 200) {
            $msg = '抓取失败fetch news cnbeta  index' . $url;
            echo $msg;
            \Log::critical($msg);
            return;
        }

        $dom = $res->getBody()->getContents();

        $crawler = new Crawler($dom);

        $crawler->filter('div.cnbeta-update-list.category-update > div.items-area > div > dl > dt > a')->each(function (Crawler $aNode, $i) {
            $url = $aNode->attr('href');
            $title = $aNode->text();
            echo $title . "\n";
            echo $url . "\n";

            $url_provider = $url;
            $page = $this->client->request('GET', $url_provider);

            if ($page->getStatusCode() == 200) {
                try {
                    $dom = $page->getBody()->getContents();
                    $page_html = new Crawler($dom);
                    $description = $page_html->filter('div.cnbeta-article > div.cnbeta-article-body > div.article-summary > p')->first()->html();
                    $body = $page_html->filter('div.cnbeta-article-body > div.article-content')->first()->html();
                    $body = $description . $body;
                    $imgUrls = [];
                    $page_html->filter('div.cnbeta-article-body > div.article-content img')->each(function ($node, $idx) use (&$imgUrls) {
                        $imgUrls[] = $node->attr('src');
                    });
                    Article::Spider_Insert($title, $body, $url_provider, $imgUrls);
                } catch (\Exception $e) {
                    $mesage = '爬去cnbeta失败:' . $url_provider . "\n" . $e->getMessage() . "\n";
                    \Log::alert($mesage);
                    echo $mesage;
                }
            }

        });


    }
}
